import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
from PyQt5.QtCore import QThread, pyqtSignal
import asyncio

class CoroutineThread(QThread):
    result_ready = pyqtSignal(str)  # 定义信号

    async def run_coroutine(self):
        await asyncio.sleep(2)
        self.result_ready.emit("协程结果")
        i=0
        while i<10:
        	print(i)

    def run(self):
        # 在子线程中运行 asyncio 任务
        asyncio.run(self.run_coroutine())

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.button = QPushButton("运行协程")
        self.button.clicked.connect(self.start_thread)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

    def start_thread(self):
        self.thread = CoroutineThread()
        self.thread.result_ready.connect(self.handle_result)
        self.thread.start()

    def handle_result(self, result):
        print(result)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())